package buildings.gui;

import buildings.Building;
import buildings.Buildings;
import buildings.dwelling.DwellingFactory;
import buildings.office.OfficeFactory;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class MainFrame extends javax.swing.JFrame {

    private Building b = null;
    private boolean isDwelling;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((screenSize.getWidth() - getWidth()) / 2);
        if (x < 0) {
            x = 0;
        }
        int y = (int) ((screenSize.getHeight() - getHeight()) / 2);
        if (y < 0) {
            y = 0;
        }
        setBounds(x, y, getWidth(), getHeight());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        radioButtonGroup = new javax.swing.ButtonGroup();
        buildingPanel = new javax.swing.JPanel();
        bTypeCaption = new javax.swing.JLabel();
        bTypeInfo = new javax.swing.JLabel();
        fCountCaption = new javax.swing.JLabel();
        fCountInfo = new javax.swing.JLabel();
        totalBuildingAreaCaption = new javax.swing.JLabel();
        totalBuildingAreaInfo = new javax.swing.JLabel();
        totalSpaceCountCaption = new javax.swing.JLabel();
        totalSpaceCountInfo = new javax.swing.JLabel();
        spacePanel = new javax.swing.JPanel();
        sNumberCaption = new javax.swing.JLabel();
        rCountCaption = new javax.swing.JLabel();
        totalSpaceAreaCaption = new javax.swing.JLabel();
        sNumberInfo = new javax.swing.JLabel();
        rCountInfo = new javax.swing.JLabel();
        totalSpaceAreaInfo = new javax.swing.JLabel();
        floorPanel = new javax.swing.JPanel();
        fNumberCaption = new javax.swing.JLabel();
        sCountCaption = new javax.swing.JLabel();
        totalFloorAreaCaption = new javax.swing.JLabel();
        fNumberInfo = new javax.swing.JLabel();
        sCountInfo = new javax.swing.JLabel();
        totalFloorAreaInfo = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        planPanel = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openDwelling = new javax.swing.JMenuItem();
        openOfficeBuilding = new javax.swing.JMenuItem();
        lookAndFeelMenu = new javax.swing.JMenu();
        defaultLookAndFeelMenuItem = new javax.swing.JMenuItem();
        metalLookAndFeelMenuItem = new javax.swing.JMenuItem();
        motifLookAndFeelMenuItem = new javax.swing.JMenuItem();
        windowsLookAndFeelMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Здания");
        setForeground(java.awt.Color.white);
        setResizable(false);

        buildingPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Здание", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));
        buildingPanel.setToolTipText("");
        buildingPanel.setName(""); // NOI18N

        bTypeCaption.setText("Тип здания:");

        bTypeInfo.setForeground(new java.awt.Color(0, 0, 255));

        fCountCaption.setText("Количество этажей:");

        fCountInfo.setForeground(new java.awt.Color(0, 0, 255));

        totalBuildingAreaCaption.setText("Общая площадь:");

        totalBuildingAreaInfo.setForeground(new java.awt.Color(0, 0, 255));

        totalSpaceCountCaption.setText("Количество помещений:");

        totalSpaceCountInfo.setForeground(new java.awt.Color(0, 0, 255));

        javax.swing.GroupLayout buildingPanelLayout = new javax.swing.GroupLayout(buildingPanel);
        buildingPanel.setLayout(buildingPanelLayout);
        buildingPanelLayout.setHorizontalGroup(
            buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buildingPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(buildingPanelLayout.createSequentialGroup()
                        .addComponent(bTypeCaption)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bTypeInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(buildingPanelLayout.createSequentialGroup()
                        .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(totalBuildingAreaCaption)
                            .addComponent(fCountCaption)
                            .addComponent(totalSpaceCountCaption))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fCountInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                            .addGroup(buildingPanelLayout.createSequentialGroup()
                                .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(totalBuildingAreaInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                                    .addComponent(totalSpaceCountInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        buildingPanelLayout.setVerticalGroup(
            buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buildingPanelLayout.createSequentialGroup()
                .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bTypeCaption, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bTypeInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fCountCaption)
                    .addComponent(fCountInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(totalSpaceCountCaption, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(totalSpaceCountInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(totalBuildingAreaInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(totalBuildingAreaCaption))
                .addContainerGap())
        );

        spacePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Помещение", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));
        spacePanel.setToolTipText("");
        spacePanel.setName(""); // NOI18N

        sNumberCaption.setText("Номер помещения:");

        rCountCaption.setText("Количество комнат:");

        totalSpaceAreaCaption.setText("Общая площадь:");

        sNumberInfo.setForeground(new java.awt.Color(0, 0, 255));

        rCountInfo.setForeground(new java.awt.Color(0, 0, 255));

        totalSpaceAreaInfo.setForeground(new java.awt.Color(0, 0, 255));

        javax.swing.GroupLayout spacePanelLayout = new javax.swing.GroupLayout(spacePanel);
        spacePanel.setLayout(spacePanelLayout);
        spacePanelLayout.setHorizontalGroup(
            spacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(spacePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(spacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sNumberCaption)
                    .addComponent(rCountCaption)
                    .addComponent(totalSpaceAreaCaption))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(spacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sNumberInfo)
                    .addComponent(rCountInfo)
                    .addComponent(totalSpaceAreaInfo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        spacePanelLayout.setVerticalGroup(
            spacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(spacePanelLayout.createSequentialGroup()
                .addGroup(spacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sNumberCaption)
                    .addComponent(sNumberInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(spacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rCountCaption)
                    .addComponent(rCountInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(spacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalSpaceAreaCaption)
                    .addComponent(totalSpaceAreaInfo))
                .addGap(0, 9, Short.MAX_VALUE))
        );

        floorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Этаж", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));
        floorPanel.setToolTipText("");
        floorPanel.setName(""); // NOI18N

        fNumberCaption.setText("Номер этажа:");

        sCountCaption.setText("Количество помещений:");

        totalFloorAreaCaption.setText("Общая площадь:");

        fNumberInfo.setForeground(new java.awt.Color(0, 0, 255));

        sCountInfo.setForeground(new java.awt.Color(0, 0, 255));

        totalFloorAreaInfo.setForeground(new java.awt.Color(0, 0, 255));

        javax.swing.GroupLayout floorPanelLayout = new javax.swing.GroupLayout(floorPanel);
        floorPanel.setLayout(floorPanelLayout);
        floorPanelLayout.setHorizontalGroup(
            floorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(floorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(floorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(totalFloorAreaCaption)
                    .addComponent(sCountCaption)
                    .addComponent(fNumberCaption))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(floorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fNumberInfo)
                    .addComponent(sCountInfo)
                    .addComponent(totalFloorAreaInfo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        floorPanelLayout.setVerticalGroup(
            floorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(floorPanelLayout.createSequentialGroup()
                .addGroup(floorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fNumberCaption)
                    .addComponent(fNumberInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(floorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sCountCaption)
                    .addComponent(sCountInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(floorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalFloorAreaCaption)
                    .addComponent(totalFloorAreaInfo))
                .addGap(0, 10, Short.MAX_VALUE))
        );

        jScrollPane2.setHorizontalScrollBar(null);

        planPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout planPanelLayout = new javax.swing.GroupLayout(planPanel);
        planPanel.setLayout(planPanelLayout);
        planPanelLayout.setHorizontalGroup(
            planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 556, Short.MAX_VALUE)
        );
        planPanelLayout.setVerticalGroup(
            planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 479, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(planPanel);

        fileMenu.setText("Файл");

        openDwelling.setText("Открыть жилой дом");
        openDwelling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openDwellingActionPerformed(evt);
            }
        });
        fileMenu.add(openDwelling);

        openOfficeBuilding.setText("Открыть офисное здание");
        openOfficeBuilding.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openOfficeBuildingActionPerformed(evt);
            }
        });
        fileMenu.add(openOfficeBuilding);

        jMenuBar1.add(fileMenu);

        lookAndFeelMenu.setText("Look&Feel");

        defaultLookAndFeelMenuItem.setText("Default");
        defaultLookAndFeelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultLookAndFeelMenuItemActionPerformed(evt);
            }
        });
        lookAndFeelMenu.add(defaultLookAndFeelMenuItem);

        metalLookAndFeelMenuItem.setText("Metal");
        metalLookAndFeelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                metalLookAndFeelMenuItemActionPerformed(evt);
            }
        });
        lookAndFeelMenu.add(metalLookAndFeelMenuItem);

        motifLookAndFeelMenuItem.setText("Motif");
        motifLookAndFeelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                motifLookAndFeelMenuItemActionPerformed(evt);
            }
        });
        lookAndFeelMenu.add(motifLookAndFeelMenuItem);

        windowsLookAndFeelMenuItem.setText("Windows");
        windowsLookAndFeelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                windowsLookAndFeelMenuItemActionPerformed(evt);
            }
        });
        lookAndFeelMenu.add(windowsLookAndFeelMenuItem);

        jMenuBar1.add(lookAndFeelMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(floorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buildingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(spacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buildingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(floorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spacePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void openDwellingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openDwellingActionPerformed
        Buildings.setBuildingFactory(new DwellingFactory());
        isDwelling = true;
        JFileChooser fileOpen = new JFileChooser();
        if (fileOpen.showDialog(null, "Выбрать файл") == JFileChooser.APPROVE_OPTION) {
            String fileName = fileOpen.getSelectedFile().getAbsolutePath();
            try {
                b = Buildings.readBuilding(new FileReader(fileName));
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Невозможно прочитать файл",
                        "Внимание!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (b == null) {
                JOptionPane.showMessageDialog(null, "Невозможно прочитать файл",
                        "Внимание!", JOptionPane.ERROR_MESSAGE);
            }
            fillBuildingPanel();
            fillPlanPanel();
        }
    }//GEN-LAST:event_openDwellingActionPerformed

    private void openOfficeBuildingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openOfficeBuildingActionPerformed
        Buildings.setBuildingFactory(new OfficeFactory());
        isDwelling = false;
        JFileChooser fileOpen = new JFileChooser();
        if (fileOpen.showDialog(null, "Выбрать файл") == JFileChooser.APPROVE_OPTION) {
            String fileName = fileOpen.getSelectedFile().getAbsolutePath();
            try {
                b = Buildings.readBuilding(new FileReader(fileName));
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Невозможно прочитать файл",
                        "Внимание!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (b == null) {
                JOptionPane.showMessageDialog(null, "Невозможно прочитать файл",
                        "Внимание!", JOptionPane.ERROR_MESSAGE);
            }
            fillBuildingPanel();
            fillPlanPanel();
        }
    }//GEN-LAST:event_openOfficeBuildingActionPerformed

    private void metalLookAndFeelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_metalLookAndFeelMenuItemActionPerformed
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        SwingUtilities.updateComponentTreeUI(this);
    }//GEN-LAST:event_metalLookAndFeelMenuItemActionPerformed

    private void motifLookAndFeelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_motifLookAndFeelMenuItemActionPerformed
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        SwingUtilities.updateComponentTreeUI(this);
    }//GEN-LAST:event_motifLookAndFeelMenuItemActionPerformed

    private void windowsLookAndFeelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_windowsLookAndFeelMenuItemActionPerformed
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        SwingUtilities.updateComponentTreeUI(this);
    }//GEN-LAST:event_windowsLookAndFeelMenuItemActionPerformed

    private void defaultLookAndFeelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultLookAndFeelMenuItemActionPerformed
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        SwingUtilities.updateComponentTreeUI(this);
    }//GEN-LAST:event_defaultLookAndFeelMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    private void fillBuildingPanel() {
        if (b == null) {
            return;
        }
        bTypeInfo.setText(isDwelling ? "жилой дом" : "офисное здание");
        fCountInfo.setText(Integer.toString(b.getFloorCount()));
        totalBuildingAreaInfo.setText(Float.toString(b.getTotalSquare()));
        totalSpaceCountInfo.setText(Integer.toString(b.getSpaceCount()));
    }

    private void fillFloorPanel(int floorNumber) {
        if (b == null) {
            return;
        }
        fNumberInfo.setText(Integer.toString(floorNumber));
        sCountInfo.setText(Integer.toString(b.getFloorByNumber(floorNumber).getSpaceCount()));
        totalFloorAreaInfo.setText(Float.toString(b.getFloorByNumber(floorNumber).getTotalSquare()));
    }

    private void fillSpacePanel(int spaceNumber) {
        if (b == null) {
            return;
        }
        sNumberInfo.setText(Integer.toString(spaceNumber));
        rCountInfo.setText(Integer.toString(b.getSpaceByNumber(spaceNumber).getRoomCount()));
        totalSpaceAreaInfo.setText(Float.toString(b.getSpaceByNumber(spaceNumber).getSquare()));
    }

    private void fillPlanPanel() {
        planPanel.setLayout(new BoxLayout(this.planPanel, BoxLayout.Y_AXIS));
        int flat = 0;
        for (int i = 0; i < b.getFloorCount(); i++) {
            JPanel panel = new JPanel();
            panel.setBorder(new TitledBorder("Этаж " + i));
            panel.setName("floorPanel" + i);
            panel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    String panelName = e.getComponent().getName();
                    int floorNumber = Integer.parseInt(panelName.substring(10, panelName.length()));
                    fillFloorPanel(floorNumber);
                }
            });
            for (int j = 0; j < b.getFloorByNumber(i).getSpaceCount(); j++) {
                JPanel sPanel = new JPanel();
                sPanel.setBorder(new EtchedBorder());
                sPanel.setName("spacePanel" + flat);
                sPanel.add(new JLabel(Integer.toString(flat)));
                panel.setLayout(new FlowLayout(FlowLayout.LEFT));
                panel.add(sPanel);
                sPanel.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        String panelName = e.getComponent().getName();
                        int spaceNumber = Integer.parseInt(panelName.substring(10, panelName.length()));
                        fillSpacePanel(spaceNumber);
                    }
                });
                flat++;
            }
            planPanel.add(panel);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bTypeCaption;
    private javax.swing.JLabel bTypeInfo;
    private javax.swing.JPanel buildingPanel;
    private javax.swing.JMenuItem defaultLookAndFeelMenuItem;
    private javax.swing.JLabel fCountCaption;
    private javax.swing.JLabel fCountInfo;
    private javax.swing.JLabel fNumberCaption;
    private javax.swing.JLabel fNumberInfo;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JPanel floorPanel;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JMenu lookAndFeelMenu;
    private javax.swing.JMenuItem metalLookAndFeelMenuItem;
    private javax.swing.JMenuItem motifLookAndFeelMenuItem;
    private javax.swing.JMenuItem openDwelling;
    private javax.swing.JMenuItem openOfficeBuilding;
    private javax.swing.JPanel planPanel;
    private javax.swing.JLabel rCountCaption;
    private javax.swing.JLabel rCountInfo;
    private javax.swing.ButtonGroup radioButtonGroup;
    private javax.swing.JLabel sCountCaption;
    private javax.swing.JLabel sCountInfo;
    private javax.swing.JLabel sNumberCaption;
    private javax.swing.JLabel sNumberInfo;
    private javax.swing.JPanel spacePanel;
    private javax.swing.JLabel totalBuildingAreaCaption;
    private javax.swing.JLabel totalBuildingAreaInfo;
    private javax.swing.JLabel totalFloorAreaCaption;
    private javax.swing.JLabel totalFloorAreaInfo;
    private javax.swing.JLabel totalSpaceAreaCaption;
    private javax.swing.JLabel totalSpaceAreaInfo;
    private javax.swing.JLabel totalSpaceCountCaption;
    private javax.swing.JLabel totalSpaceCountInfo;
    private javax.swing.JMenuItem windowsLookAndFeelMenuItem;
    // End of variables declaration//GEN-END:variables
}
